import lombok.Data;

/**
 * 单向链表判断链表中是否出现了环
 */
public class LinkTable {
    public static void main(String[] args) {
        Node a = new Node("A");
        Node b = new Node("B");
        Node c = new Node("C");
        Node d = new Node("D");

        a.setNext(b);
        b.setNext(c);
        c.setNext(d);
        d.setNext(a);

        System.out.println(hasCycle(a));

    }

    public static boolean hasCycle(Node head){

        if(head == null){
            return false;
        }
        Node temp = head;

        while(temp.next != null){

            if(temp.next == head){
                return true;
            }

            temp = temp.next;
        }

        return false;
    }

    @Data
    public static class Node{
        private Object value;
        private Node next;

        public Node(Object value){
            this.value = value;
        }
    }
}
